/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wider.foundation.ui;

import java.io.PrintStream;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.JTextComponent;

/**
 * This class defines an adapter which adapt the normal use of PrintStream to be output on the Swing JTextPane (/JTextArea) component.
 * @author William Choi <avextk@gmail.com>
 */
public class JTextPrintStream extends PrintStream{
    private interface JTextWrapperImpl{
        void write(String content);
    }
    private JTextComponent  jtextcom;
    private JTextWrapperImpl   wrapper;

    public JTextPrintStream(JTextComponent jtext){
        super(System.out);
        jtextcom = jtext;
        if (jtext instanceof JTextPane){
            wrapper = new JTextWrapperImpl(){
                @Override
                public void write(String content) {
                    JTextPane jtextpane = (JTextPane) jtextcom;
                    jtextpane.setText(jtextpane.getText() + content);
                }
            };
        }else if (jtext instanceof JTextArea){
            wrapper = new JTextWrapperImpl(){
                @Override
                public void write(String content) {
                    ((JTextArea) jtextcom).append(content);
                }
            };
        }else{
            throw new IllegalArgumentException("JTextPrintStream MUST be constructed with instance of JTextPane or JTextArea");
        }
    }

    @Override
    public void print(String s){
        wrapper.write(s);
    }

    public void print(Object obj){
        wrapper.write(obj.toString());
    }

    public void print(long l){
        wrapper.write(String.valueOf(l));
    }

    public void print(int i){
        wrapper.write(String.valueOf(i));
    }

    public void print(float f){
        wrapper.write(String.valueOf(f));
    }

    public void print(double d){
        wrapper.write(String.valueOf(d));
    }

    public void print(char[] s){
        wrapper.write(String.valueOf(s));
    }

    public void print(char c){
        wrapper.write(String.valueOf(c));
    }

    public void print(boolean b){
        wrapper.write(String.valueOf(b));
    }

    public void println(){
        wrapper.write("\n");
    }

    public void println(String s){
        print(s);
        println();
    }

    public void println(Object obj){
        print(obj);
        println();
    }

    public void println(long l){
        print(l);
        println();
    }

    public void println(int i){
        print(i);
        println();
    }

    public void println(float f){
        print(f);
        println();
    }

    public void println(double d){
        print(d);
        println();
    }

    public void println(char[] s){
        print(s);
        println();
    }

    public void println(char c){
        print(c);
        println();
    }

    public void println(boolean b){
        print(b);
        println();
    }
}
